require 'active_support'
namespace :assets do
  desc "Build list of desktop images"
  task :desktop do
    catalog_file "bin/system/desktop_backgrounds", "DesktopCatalog"
  end

  desc "App-related assets"
  task :apps do
    Dir.chdir("src/plugins/apps/firefox/") do
      asset_file ".", "FirefoxLibrary", :package=>'plugins.apps.firefox.assets'
    end
  end
  
  desc "Create boot classes"
  task :boot do
    asset_file "boot", "BootLibrary"
    asset_file "boot/avatars", "Avatars"
    asset_file "boot/icons", "BootIcons"
  end
  
  desc "mouse cursors"
  task :cursors do
    asset_file "cursors", "CursorLibrary"
  end
  
  desc "Create menu icon classes"
  task :menu do
    Dir.chdir("common/plugins/menus") do
      asset_file '.', 'MessagingServicesLibrary', :package=>'plugins.menus.assets'
    end
  end

  desc "Create menu panel icon classes"
  task :panels do
    asset_file "panels", "PanelLibrary"
  end

  desc "Create UI library"
  task :ui do
    asset_file "ui", "UILibrary"
    asset_file "ui/scrollbar", "OverlayScrollbarLibrary"
  end
  
  desc "testing assets"
  task :testing do
    asset_file "testing", "TestingLibrary"
  end
  
  desc 'launcher assets'
  task :launcher do
    Dir.chdir("common/plugins/launcher") do
      asset_file '.', 'LauncherLibrary', :package=>'plugins.launcher.assets'
    end
  end

  desc 'dash-related assets'
  task :dash do
    Dir.chdir("common/plugins/dash") do
      asset_file '.', 'DashLibrary', :package=>'plugins.dash.assets'
      asset_file './categories', 'DashCategoriesLibrary', :package=>'plugins.dash.assets.categories'
      asset_file './groupicons', 'DashGroupIconsLibrary', :package=>'plugins.dash.assets.groupicons'
      asset_file './menuicons', 'DashMenuIconsLibrary', :package=>'plugins.dash.assets.menuicons'

    end
  end
  
  desc 'hud-related assets'
  task :hud do
    asset_file 'hud', 'HUDLibrary'
  end
  
  desc 'unr-related assets'
  task :unr do
    asset_file 'unr', "UNRLibrary"
  end
  
  desc "window assets"
  task :theme do
   Dir.entries("common/themes/").each do |entry|
      
      if File.directory?("common/themes/#{entry}") && (entry =~ /^\./).nil? && entry != "fonts"
        puts "updating theme library for #{entry} to #{entry.classify}"
        Dir.chdir("common/themes/#{entry}") do
          asset_file ".", "#{entry.classify}Library", :package=>"themes.#{entry}.assets"
        end
      end
    end
  end
  
  def asset_file dir, class_name, options={}
    Dir.chdir("assets/#{dir}") do
      File.open("#{class_name}.as","w") do |file|
        file << <<-END
        package #{options[:package] || dir.gsub("/",".")}{
          public class #{class_name}{
        END
        %w[.png .jpg .swf].each do |suffix|
          add_entries file, suffix
        end
        file << "}}"
      end
    end
    
  end
  def add_entries file, suffix
    Dir.entries(".").select{ |f| f.match(suffix) }.each do |icon|
      file << "[Embed(source='#{icon}')] "
      file << "public static var #{icon.gsub(suffix,"").gsub("-","_")}: Class;\n"
      puts icon
    end
  end
   
   def catalog_file dir, class_name
     File.open("assets/catalogs/#{class_name}.as","w") do |file|
      file << <<-END
      package catalogs {
      	// this file is automatically generated with rake assets:desktop
      	public class DesktopCatalog {
      		public static var BACKGROUNDS : Array = [
        END
      
      file << Dir.entries(dir).map{|f| "'#{f}'" unless f =~ /^\./ }.compact.join(",")
        
      file << <<-END
      		];
      	}
      }
      END
    end
   end
   
end